<!--
 * @Description: 
 * @FilePath: \rs_wms_pc\src\views\invoice\preparation\distributeDialog.vue
-->
<template>
    <!-- 下发对话框 -->
    <el-dialog title="下发订单" :visible.sync="visible" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="工单编号" prop="no">
          <el-input readonly v-model="form.workOrderNo" />
        </el-form-item>
        <el-form-item label="工单数量" prop="workOrderQty">
          <el-input readonly v-model="form.workOrderQty" />
        </el-form-item>
        <el-form-item label="已发工单数量" prop="issuedQty">
          <el-input readonly v-model="form.issuedQty" />
        </el-form-item>
        <el-form-item label="未下工单数量" prop="notSentQty">
          <el-input readonly v-model="form.notSentQty" />
        </el-form-item>
        <el-form-item label="库存可满足工单数量" prop="stockQty">
          <div class="flex-row">
            <el-input readonly v-model="form.stockQty" />
            <el-button type="primary" @click="getDistributeInfo" style="margin-left: 8px;">刷新</el-button>
          </div>
        </el-form-item>
        <el-form-item label="可下发工单数量" prop="assignedQty">
          <el-input readonly v-model="form.assignedQty" />
        </el-form-item>
        <el-form-item :label="$t('inOutStock.outStockOrder.pickerName')"  prop="picker">
          <el-select style="width:100%;" v-model="form.picker" filterable :placeholder="$t('selectTip') + $t('inOutStock.outStockOrder.pickerName')">
            <el-option
              v-for="dict in userList"
              :key="dict.userName"
              :label="dict.nickName"
              :value="dict.userName"
              @click.native="form.pickerName = dict.nickName"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- 
        <el-form-item label="仓库" prop="storehouseNo">
          <el-select style="width:100%;" v-model="form.storehouseNo" filterable placeholder="请选择仓库">
            <el-option
              v-for="dict in storehouseList"
              :key="dict.no"
              :label="dict.name"
              :value="dict.no"
              @click.native="changeStorehouse(dict, true)"
            ></el-option>
          </el-select>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
</template>


<script>

import { getStorehouseAreaPicker  } from "@/api/inOutStock/outStockDetailOrder";
import { getDistributeInfo, getStockQty, confirmDistribute } from "@/api/invoice/preparation";
import { listUser } from "@/api/system/user";

export default {
  name: "DistributeDialog",
  inject: ['refresh'],
  props:{
    info:{
      type: Object,
      default:()=>{}
    }
  },
  data() {
    return {
      visible:false,
      form:{},
      // 表单校验
      rules: {
        picker: [
          { required: true, message: "拣货员不能为空", trigger: "blur" }
        ]
      },
      // 用户列表
      userList: [],
    }
  },
  watch:{
    visible(newV){
      if(newV){
        this.getDistributeInfo();
        this.getStorehouseAreaPicker()
      }
    }
  },
  created(){},
  methods:{
      // 查询库区对应的拣货员
      getStorehouseAreaPicker(){
        getStorehouseAreaPicker({
          storehouseArea: this.info.storehouseAreaNo,
          pageSize: 0
        }).then(res => {
          if (res.code == 200){
            if(res.total > 0){
              res.rows.forEach((item) => {
                item.userName = item.shipper;
                item.nickName = item.shipperName;
              });
              this.$set(this,'userList',res.rows);
              this.$set(this.form,'picker',res.rows[0].userName)
            }else{
              this.getUserList()
            }
          }
        });
      },
      // 获取用户列表
      getUserList(){
        listUser({pageSize:0,status:0}).then(response => {
          this.userList = response.rows;
        });
      },
    // 获取信息
    getDistributeInfo(){
      getDistributeInfo(this.info.id).then(response=>{
        this.form = response.data;
      })
    },
    // // 刷新库存可满足数量  （现在直接刷新整个下发信息）
    // getStockQty(){
    //   getStockQty(this.info.id).then(res=>{
    //     this.$set(this.form, 'stockQty', res.data)
    //   })
    // },
    // 取消
    cancel(){
      this.visible = false
    },
    submitForm(){
      if(!this.form.assignedQty){
        this.$modal.msgError("可下发工单数量为0，不可下发");
        return;
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          confirmDistribute(this.form).then(res=>{
            this.$modal.msgSuccess("下发成功");
            this.refresh()
            this.cancel()
          })
        }
      })
    },
  }
}
</script>